package custom;

import entity.MessageData;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author:Eric
 * @date:2021/8/4 11:38
 */
@Slf4j
public class ClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("客户端第一次给服务器发送消息");
        ctx.writeAndFlush(new MessageData().setId(1L).setName("eric").setState(0));
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        String string = msg.toString();
        log.info("收到服务器消息:{}", string);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        Thread.sleep(3000);
        log.info("客户端给服务器发送消息");
        ctx.writeAndFlush(new MessageData().setId(2L).setName("张三").setState(1));
    }
}
